/**
 * 包含应用中所有接口请求函数的模块
 * 每个函数的返回值都是promise
 */
import ajax from './ajax'
import jsonp from 'jsonp'
import {
    message
} from 'antd'

const BASE = ''
//这里的 箭头函数后面 不用加{} 如果加了{}，就需要 加 return
// (username,password) =>{return ajax(url,{username,password},"POST")}
export const reqLogin = (username, password) => ajax(BASE + '/login', {
    username,
    password
}, 'POST')

export const reqAddUser = (user) => ajax(BASE + "/manage/user/add", {
    user
}, "POST")

// 获取 一级/二级 分类的列表
export const reqCategorys = (parentId) => ajax(BASE + '/manage/category/list', {
    parentId
}, 'GET')
//添加分类
export const reqAddCategory = (parentId, categoryName) => ajax(BASE + '/manage/category/add', {
    parentId,
    categoryName
}, 'POST')
//更新分类
export const reqUpdateCategory = (categoryId, categoryName) => ajax(BASE + '/manage/category/update', {
    categoryId,
    categoryName
}, 'POST')

export const getAesList = () => ajax(BASE + '/list', {}, 'POST')
/*
通过 jsonp 请求获取天气信息
*/
export function reqWeather(city) {
    const url =
        `http://api.map.baidu.com/telematics/v3/weather?location=${city}&output=json&ak=3p49MVra6urFRGOT9s8UBWr2`
    return new Promise((resolve, reject) => {
        jsonp(url, {
            param: 'callback'
        }, (error, response) => {
            if (!error && response.status === 'success') {
                const {
                    dayPictureUrl,
                    weather
                } = response.results[0].weather_data[0]
                resolve({
                    dayPictureUrl,
                    weather
                })
            } else {
                message.error(' 获取天气信息失败')
            }
        })
    })
}